package com.sand.service.Imp;

import com.alibaba.fastjson.JSON;
import com.sand.dao.IBaseDao;
import com.sand.entity.TRoleEntity;
import com.sand.entity.TRoleuserEntity;
import com.sand.entity.TUserEntity;
import com.sand.service.IUserService;
import com.sand.util.PageModel;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

@Service(value = "userService")
public class UserServiceImp implements IUserService {
/**
 *用户控制层
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/11/28/028 11:34
 */
    //Dao层接口
    @Resource(name = "baseDao")
    private IBaseDao baseDao;

    /**
     * 根据用户登录名和密码查询用户信息,状态为启用（1）的用户
     *
     * @param name
     * @param pwd
     * @return
     */
    @Override
    public TUserEntity queryUserByNamePwd(String name, String pwd) {
        List<TUserEntity> list = baseDao.queryObject("from TUserEntity t where t.loginName=?0 and t.password=?1 and t.status=?2", name,pwd,1);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    /**
     * 分页查询用户
     *
     * @param limit
     * @param page
     * @param condition
     * @return
     */
    @Override
    public PageModel<TUserEntity> queryUserLimit(int limit, int page, String condition) {
        PageModel<TUserEntity> pm = new PageModel<TUserEntity>();
        //计算开始位置
        int begin=(page-1)*limit;
        //创建查询对象
        DetachedCriteria dc = DetachedCriteria.forClass(TUserEntity.class);
        //添加模糊查询条件
        if(condition!=null){
            dc.add(Restrictions.like("userName","%"+condition+"%"));
        }

        List<Long> list1 = baseDao.queryObjectCommon(dc.setProjection(Projections.rowCount()));
        dc.setProjection(null);
        dc.addOrder(Order.desc("rid"));
        List<TUserEntity> list = baseDao.queryObjectLimit(dc, begin, limit);
        long i=list1.get(0);
        int count= (int) i;

        List<TRoleEntity> list2 = baseDao.queryObject("from TRoleEntity");
        if(list2!=null && list!=null){
            for (TUserEntity t : list) {
                for (TRoleEntity r : list2) {
                    if(t.getPost()==r.getRid()){
                        t.setPostView(r.getName());
                    }
                }
            }
        }

        pm.setList(list);
        pm.setPageNo(page);
        pm.setPageSize(limit);
        pm.setPagecount(count);
        return pm;
    }

    /**
     * 添加用户
     *
     * @param data
     * @return
     */
    @Override
    public boolean addUser(String data) {
        //创建时间
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        if(data!=null){
            TUserEntity user = JSON.parseObject(data, TUserEntity.class);
            //获取角色编号
            int post=user.getPost();
            user.setCreateTime(timestamp);
            boolean bln = baseDao.addObject(user);
            if(bln){
                //添加角色信息
                TRoleuserEntity roleUser = new TRoleuserEntity();
                roleUser.setRoleId(post);
                roleUser.setUserId(user.getRid());
                boolean b = baseDao.addObject(roleUser);
                if(b){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 删除用户
     *
     * @param data
     * @return
     */
    @Override
    public boolean delUser(String data) {
        if(data!=null){
            //解析json数据
            List<TUserEntity> list = JSON.parseArray(data, TUserEntity.class);
            boolean bln = baseDao.delAllObject(list);
            if(bln){
                return true;
            }
        }
        return false;
    }

    /**
     * 修改用户
     *
     * @param data
     * @return
     */
    @Override
    public boolean updateUser(String data) {
        if(data!=null){
            TUserEntity user = JSON.parseObject(data, TUserEntity.class);
            boolean bln = baseDao.updateObject(user);
            if(bln){
                List<TRoleuserEntity> list = baseDao.queryObject("from TRoleuserEntity t where t.userId=" + user.getRid());
                TRoleuserEntity roleUser = list.get(0);
                roleUser.setRoleId(user.getPost());
                boolean b = baseDao.updateObject(roleUser);
                if(b){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 根据用户角色ID查询用户
     *
     * @param id
     * @return
     */
    @Override
    public TUserEntity queryUserById(int id) {
        TUserEntity user = (TUserEntity) baseDao.queryObjectById(TUserEntity.class, id);
        List<TRoleEntity> list2 = baseDao.queryObject("from TRoleEntity");
        if(list2!=null && user!=null){
            for (TRoleEntity r : list2) {
                if(user.getPost()==r.getRid()){
                    user.setPostView(r.getName());
                }
            }
        }
        return user;
    }
}
